package com.jwgf.smarowindingmachine_unity.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import com.jwgf.Half485.modbusthreadpool.ModbusCommandManager;
import com.jwgf.Half485.modbusthreadpool.ModbusUtils;
import com.jwgf.Half485.modbusthreadpool.PLCData;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class LockDialog extends DialogFragment {

    private AlertDialog dlg;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final EditText edit = new EditText(getActivity());
        edit.setImeOptions(EditorInfo.IME_ACTION_DONE);
        edit.setSingleLine();
        edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    String password = v.getText().toString();
                    try {
                        ByteBuffer buffer = ByteBuffer.wrap(password.getBytes("utf-8"));
                        if (password.length() == 6) {
                            ModbusCommandManager.getInstance().putPriorityCmd(ModbusUtils.createWriteRegisterCmd((byte) 1, 734, buffer.array()));
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

                return false;
            }
        });

        StringBuilder stringBuilder = new StringBuilder();
        int plcId_1 = PLCData.getInstance().getHoldingRegister(1, 698);
        int plcId_2 = PLCData.getInstance().getHoldingRegister(1, 699);
        int plcId_3 = PLCData.getInstance().getHoldingRegister(1, 700);
        int plcId_4 = PLCData.getInstance().getHoldingRegister(1, 701);
        int plcId_5 = PLCData.getInstance().getHoldingRegister(1, 702);
        int plcId_6 = PLCData.getInstance().getHoldingRegister(1, 703);
        stringBuilder.append((char) ((plcId_1 >> 8) & 0xFF))
                .append((char) (plcId_1 & 0xFF))
                .append((char) ((plcId_2 >> 8) & 0xFF))
                .append((char) (plcId_2 & 0xFF))
                .append((char) ((plcId_3 >> 8) & 0xFF))
                .append((char) (plcId_3 & 0xFF))
                .append((char) ((plcId_4 >> 8) & 0xFF))
                .append((char) (plcId_4 & 0xFF))
                .append((char) ((plcId_5 >> 8) & 0xFF))
                .append((char) (plcId_5 & 0xFF))
                .append((char) ((plcId_6 >> 8) & 0xFF))
                .append((char) (plcId_6 & 0xFF));
        String title = stringBuilder.toString();
        builder.setTitle(title);
        builder.setView(edit);
        dlg = builder.create();
        dlg.setCanceledOnTouchOutside(false);
        return dlg;
    }


}
